<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
</head>
<body>
    <h1>秒杀活动123</h1>
    <div>
        <table border="1">
            <tbody id="commodities">
              
            </tbody>
        </table>
        <div>
            <h3 id="message"></h3>
        </div>
    </div>
    <script src="./js/jquery-1.12.4.min.js"></script>
    <script>
        
        let userId;
        window.onload = function(){
            $.ajax({
                url:"/api/shop-commodity/commodities/1",
                method:"get",
                success(data){
                    console.log(data);
                    $("#commodities").append(`
                        <tr>
                            <td>${data.data.commName}</td>
                            <td>${data.data.commPrice}</td>
                            <td><input type="button" value="购买" onclick="buy(${data.data.commId})"></td>
                        </tr>
                    `);
                }
            })
            getUser();
        }
        function buy(commId){
            // 判断用户id是否存在，如果不存在则不能购买
            if(!userId){
                $("#message").html("请先登录")
                return;
            }
            $.ajax({
                url:"/api/shop-order/orders/seckill",
                method:"post",
                data:{
                    userId,commId
                },
                success(){
                    $("#message").html("提交成功，请等待...");
                    getOrderStatus(commId); 
                }
            });
        }
        function getUser(){
            $.ajax({
                url:"/api/shop-user/users/token",
                method:"get",
                success(data){
                    console.log("data",data)
                    userId = data.data.userId;
                }
            })
        }
        let intervalId = 0;
        // 等待的超时计时器
        let intervalCount = 10;
        function getOrderStatus(commId){
            intervalId = setInterval(() => {
                $.ajax({
                    url:"/api/shop-order/orders/status",
                    method:"get",
                    data:{
                        userId,commId
                    },
                    success(data){
                        intervalCount--;
                        if(data.status == 1){
                            window.location = "order.html"
                        }else if(data.status != 4002){
                            $("#message").html("抢购失败");
                            clearInterval(intervalId);
                        }else if(intervalCount < 0){
                            $("#message").html("抢购失败");
                            clearInterval(intervalId);
                        }
                        
                        
                    }
                })
            },5000)
            
        }
    </script>
</body>
</html>